package com.seari.controller;

import java.util.List;
import java.util.Map;

import com.seari.pojo.SHlineStationInfo;
import com.seari.service.LineStationInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.utils.IdWorker;
import com.seari.bean.DataGridBean;
import com.seari.bean.FaultWarningResponseBean;
import com.seari.pojo.FaultData;
import com.seari.service.FaultService;
import com.seari.utils.DateUtil;
import com.seari.utils.StringHelper;

@Controller
@RequestMapping("fault/")
public class FaultController {

    @Autowired
    private FaultService faultService;

    @Autowired
    LineStationInfoService lineStationInfoService;

    @RequestMapping("getIdGroupStatu")
    @ResponseBody
    public DataGridBean<FaultData> getIdGroupStatu() {
        DataGridBean<FaultData> dataDataGridBean = new DataGridBean<>();
        List<FaultData> faultDataList = faultService.getIdGroupStatu();
        dataDataGridBean.setTotal(faultDataList.size());
        dataDataGridBean.setRows(faultDataList);
        dataDataGridBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
        return dataDataGridBean;
    }

    /**
     * 根据线路id查询故障应急警报列表
     *
     * @return
     */
    @GetMapping("faultWarningList")
    @ResponseBody
    public DataGridBean<FaultWarningResponseBean> queryFaultWarningList(String lineId,String faultLocation ,String page,String limit) {
        DataGridBean<FaultWarningResponseBean> dataDataGridBean = new DataGridBean<>();
        List<FaultWarningResponseBean> faultDataList = faultService.queryFaultWarningList(lineId,faultLocation,page,limit);
        if(faultDataList.size() > 0){
            dataDataGridBean.setTotal(faultDataList.get(0).getCount());
        } else {
            dataDataGridBean.setTotal(0);
        }
        //使用msg字段来传“应急事件处理完成数”
        dataDataGridBean.setMsg(faultService.faultWarningCompleteCount(lineId,faultLocation));
        dataDataGridBean.setRows(faultDataList);
        dataDataGridBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
        return dataDataGridBean;
    }


    /**
     * 添加故障点
     *
     * @return
     */
    @GetMapping("addFaultPoint")
    @ResponseBody
    public DataGridBean<Object> addFaultPoint(String faultLine, String faultContent, String faultType, String faultLocation) {
        DataGridBean<Object> grid = new DataGridBean<>();

        if (StringUtils.isBlank(faultLine) || StringUtils.isBlank(faultContent)) {

            grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            grid.setMsg("故障内容或故障线路不能为空");

        } else {
            FaultData fd = new FaultData();

            fd.setFaultID(String.valueOf(new IdWorker(1, 1, 1).nextId()));
            fd.setFaultContent(faultContent);
            fd.setFaultLine(faultLine);
            fd.setFaultTime(DateUtil.getCurrDate("yyyyMMddHHmmss"));
            //0为处理完成,1已处理完成,2未开始流程
            fd.setFaultStatu(2);
            //1小电科数据, 2录入数据
            fd.setDataType("2");

            if (StringUtils.isNotBlank(faultLocation))
                fd.setFaultLocation(faultLocation);
            if (StringUtils.isNotBlank(faultType))
                fd.setFaultType(faultType);

            int result = faultService.addFaultPoint(fd);
            if (result > 0) {
                grid.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                grid.setMsg("故障点添加成功");
            } else {
                grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                grid.setMsg("故障点添加失败");
            }
        }
        return grid;
    }


    @RequestMapping("stationInfo")
    @ResponseBody
    public DataGridBean<Object> fetchLineStationInfo() {
        DataGridBean<Object> grid = new DataGridBean<>();
        List<Map<Integer, List<SHlineStationInfo>>> lineStationInfo = lineStationInfoService.fetchLineStationInfo();
        grid.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
        grid.setMsg("站点信息查询成功");
        grid.setData(lineStationInfo);
        return grid;
    }

    
    
    
}
